package com.nip.irregularvebsdict.widgets;

import java.io.InputStream;
import java.util.List;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.provider.AlarmClock;
import android.widget.RemoteViews;

import com.nip.irregularvebsdict.R;
import com.nip.irregularvebsdict.items.DateTimeItem;
import com.nip.irregularvebsdict.items.IrregularVerbItem;
import com.nip.irregularvebsdict.services.IrregularVersService;
import com.nip.irregularvebsdict.services.ServicesUtilities;
import com.nip.irregularvebsdict.ui.NIPIrregularVebsDictActivity;
import com.nip.irregularvebsdict.utilities.IrregularVerbsParser;
import com.nip.irregularvebsdict.utilities.RandomUtilities;
import com.nip.irregularvebsdict.utilities.assets.AssetsUtilities;

public class IrregularVerbRandomWidget extends AppWidgetProvider {

	private static RemoteViews remoteViews;
	private static ComponentName componentName;
	private static AppWidgetManager manager;
	private static Integer[] bgResource = { R.drawable.bg_widget_01,
			R.drawable.bg_widget_02, R.drawable.bg_widget_03,
			R.drawable.bg_widget_04, R.drawable.bg_widget_04,
			R.drawable.bg_widget_03, R.drawable.bg_widget_02,
			R.drawable.bg_widget_01 };
	private static Integer[] bgBelowResource = { R.drawable.bg_widget_below_01,
			R.drawable.bg_widget_below_02, R.drawable.bg_widget_below_03,
			R.drawable.bg_widget_below_04, R.drawable.bg_widget_below_05,
			R.drawable.bg_widget_below_06, R.drawable.bg_widget_below_07,
			R.drawable.bg_widget_below_08 };
	private static List<IrregularVerbItem> sources;
	static IrregularVerbItem verbItem;
	Intent intent;

	@Override
	public void onDisabled(Context context) {

		if (sources != null) {
			sources.clear();
		}
		if (intent != null) {
			context.stopService(intent);
		}
		super.onDisabled(context);
	}

	@Override
	public void onEnabled(Context context) {
		intent = new Intent(context, IrregularVersService.class);
		context.startService(intent);
		super.onEnabled(context);
	}

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {

		if (sources == null) {
			new IrregularVerbs().execute(AssetsUtilities.getInputStream(
					context, "garenal.xml"));
		}
		intent = new Intent(context, IrregularVersService.class);
		context.startService(intent);
		update(context, ServicesUtilities.getDateTime());

		super.onUpdate(context, appWidgetManager, appWidgetIds);
	}

	public static void update(Context context, DateTimeItem dateTimeItem) {
		if (remoteViews == null) {
			remoteViews = new RemoteViews(context.getPackageName(),
					R.layout.widget_irregular_verb_layout);
			remoteViews.setInt(R.id.bgWidget, "setBackgroundResource",
					R.drawable.bg_widget_03);
			remoteViews.setOnClickPendingIntent(R.id.bgBellowWidget,
					startApplication(context));
			remoteViews.setOnClickPendingIntent(R.id.tvWTime,
					startAlramClock(context));
		}

		if (dateTimeItem != null) {
			remoteViews.setTextViewText(R.id.tvWTime, dateTimeItem.getTime());
			remoteViews.setTextViewText(R.id.tvWDate, dateTimeItem.getDate());
			if (dateTimeItem.getIsAM()) {
				remoteViews.setInt(R.id.tvWAM, "setTextColor", context
						.getResources().getColor(R.color.white));
				remoteViews.setInt(R.id.tvWPM, "setTextColor", context
						.getResources().getColor(R.color.sliver));
			} else {
				remoteViews.setInt(R.id.tvWPM, "setTextColor", context
						.getResources().getColor(R.color.white));
				remoteViews.setInt(R.id.tvWAM, "setTextColor", context
						.getResources().getColor(R.color.sliver));
			}
		}

		pushWidgetUpdate(context, remoteViews);
	}

	public static void update(Context context) {
		if (remoteViews == null) {
			remoteViews = new RemoteViews(context.getPackageName(),
					R.layout.widget_irregular_verb_layout);
			remoteViews.setOnClickPendingIntent(R.id.bgBellowWidget,
					startApplication(context));
			remoteViews.setOnClickPendingIntent(R.id.tvWTime,
					startAlramClock(context));
		}

		remoteViews.setInt(R.id.bgWidget, "setBackgroundResource",
				bgResource[RandomUtilities.Random(0, 7)]);
		remoteViews.setInt(R.id.bgBellowWidget, "setBackgroundResource",
				bgBelowResource[RandomUtilities.Random(0, 7)]);

		if (sources != null) {
			verbItem = sources
					.get(RandomUtilities.Random(0, sources.size() - 1));
			remoteViews.setTextViewText(R.id.tvWVerb1, verbItem.getBase());
			remoteViews.setTextViewText(R.id.tvWVerbPI1,
					verbItem.getPastsimple());
			remoteViews.setTextViewText(R.id.tvWVerbPII1,
					verbItem.getPastparticiple());

			verbItem = sources
					.get(RandomUtilities.Random(0, sources.size() - 1));
			remoteViews.setTextViewText(R.id.tvWVerb2, verbItem.getBase());
			remoteViews.setTextViewText(R.id.tvWVerbPI2,
					verbItem.getPastsimple());
			remoteViews.setTextViewText(R.id.tvWVerbPII2,
					verbItem.getPastparticiple());
		}else
		{
			new IrregularVerbs().execute(AssetsUtilities.getInputStream(
					context, "garenal.xml"));
		}

		pushWidgetUpdate(context, remoteViews);
	}

	private static void pushWidgetUpdate(Context context,
			RemoteViews remoteViews) {
		componentName = new ComponentName(context,
				IrregularVerbRandomWidget.class);
		manager = AppWidgetManager.getInstance(context);
		manager.updateAppWidget(componentName, remoteViews);
	}

	private static PendingIntent startApplication(Context context) {
		Intent intent = new Intent(context, NIPIrregularVebsDictActivity.class);

		return PendingIntent.getActivity(context, 0, intent, 0);
	}

	private static PendingIntent startAlramClock(Context context)
	{
		Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM);
		
		return PendingIntent.getActivity(context, 0, intent, 0);
	}
	static class IrregularVerbs extends
			AsyncTask<InputStream, Void, List<IrregularVerbItem>> {

		@Override
		protected List<IrregularVerbItem> doInBackground(InputStream... params) {
			if (params[0] == null) {
				return null;
			}
			return new IrregularVerbsParser(params[0]).getIrregularVerbItems();
		}

		@Override
		protected void onPostExecute(List<IrregularVerbItem> result) {
			AssetsUtilities.close();
			sources = result;
			if (result == null) {
				return;
			}
			super.onPostExecute(result);
		}
	}
}
